<script setup>
import { ref, onMounted, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useUserStore } from "../stores/user";
import { navMap } from "./navConfig";
import { serviceUrl } from "@/utils/request";

const router = useRouter();
const route = useRoute();
const activeNav = ref("home");
const userStore = useUserStore();
const userInfo = ref(null);

const handleLogout = () => {
  userStore.logout();
  router.push("/welcome");
};

const handleClick = (name) => {
  emit("select", name);
  setActive(name);
};

const setActive = (nav) => {
  if (activeNav.value != nav) {
    activeNav.value = nav;

    console.log("当前激活的导航", activeNav.value);
  }
};

defineProps({
  userType: { type: Number, default: 0 }, // 0 游客
  activeNav: { type: String, required: true },
});

const emit = defineEmits(["select"]);

onMounted(() => {
  setActive(route.path);
  userInfo.value = userStore.userInfo?.record;
  console.log("用户信息", userStore.userInfo);
  console.log(route.path, userStore.userType);
});

watch(
  () => route.path,
  (newPath) => {
    if (newPath.includes("/manageCenter")) {
      console.log("来到了管理端");
      setActive("manageCenter");
    }

    userStore.initUserFromStorage();
    userInfo.value = userStore.userInfo?.record;
  },
  { immediate: true }
);
</script>

<template>
  <nav class="navbar">
    <div class="logo">博客系统</div>
    <div class="nav-links">
      <template
        v-for="item in navMap[userInfo?.userType] || navMap[0]"
        :key="item.name"
      >
        <router-link
          :to="item.path"
          :class="{ active: activeNav === item.name }"
          @click="handleClick(item.name)"
        >
          <el-icon v-if="item.icon"><component :is="item.icon" /></el-icon>
          {{ item.label }}
        </router-link>
      </template>
    </div>
    <div class="user-info">
      <template v-if="userStore.isLoggedIn">
        <span class="username">{{ userInfo.username }}</span>
        <el-avatar
          shape="square"
          :size="40"
          fit="cover"
          :src="userStore.userInfo.avatarPath ? serviceUrl + userStore.userInfo.avatarPath  : 'background.jpg'"
        />
        <el-popconfirm
          class="box-item"
          title="确定要退出系统吗？"
          placement="bottom-end"
          confirm-button-text="确定"
          cancel-button-text="取消"
          @confirm="handleLogout"
        >
          <template #reference>
            <el-button class="logout-btn" type="danger" plain
              >退出登录</el-button
            >
          </template>
        </el-popconfirm>
      </template>
      <template v-else>
        <router-link to="/login" class="login-link">登录</router-link>
      </template>
    </div>
  </nav>
</template>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background-color: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
  color: #333;
}

.nav-links {
  display: flex;
  gap: 2rem;
}

.nav-links a {
  color: #666;
  text-decoration: none;
  font-weight: 500;
  padding: 0.5rem 0;
  transition: color 0.3s ease;
}

.nav-links a:hover,
.nav-links a.active {
  color: cornflowerblue;
  border-bottom: 2px solid #646cff;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.username {
  font-weight: 500;
  color: #333;
  font-size: 18px;
}

.login-link {
  color: cornflowerblue;
  text-decoration: none;
  font-weight: 500;
}
</style>
